<template>
	<el-card>
		<el-form :inline="true" :model="state.queryForm">
			<el-row>
				<el-col :span="6">
					<el-form-item v-if="type !== 'client'" label="Client 跟进客户">
						<el-input v-model="state.queryForm.customerName" placeholder="please input a keyword" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="18">
					<el-form-item>
					<el-button @click="getDataList">Query 查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="addOrUpdateHandle()">Add新增</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="danger" @click="deleteBatchHandle()">Delete 删除</el-button>
					</el-form-item>
				</el-col>
			</el-row>
			
		</el-form>
		<el-table
			v-loading="state.dataListLoading"
			:data="state.dataList"
			border
			style="width: 100%"
			@selection-change="selectionChangeHandle"
			@sort-change="sortChangeHandle"
		>
			<el-table-column type="selection" label="No. 序号" header-align="center" align="center" width="50"></el-table-column>
			<el-table-column prop="followUpDate" label="Follow up date 跟进日期" header-align="center" align="center" :formatter="followUpDateFomat"> </el-table-column>
			<el-table-column prop="customerName" label="Client 跟进客户" header-align="center" align="center"> </el-table-column>
			<el-table-column prop="followerName" label="Follower 跟进人" header-align="center" align="center"> </el-table-column>
			<el-table-column prop="method" label="Method 跟进方式" header-align="center" align="center"></el-table-column>
			<el-table-column prop="progress" label="Progress 跟进情况" header-align="center" align="center"></el-table-column>
			<el-table-column prop="remark" label="Remark 备注" header-align="center" align="center"></el-table-column>
			<el-table-column label="Operation操作" fixed="right" header-align="center" align="center" width="170">
				<template #default="scope">
					<el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">Update修改</el-button>
					<el-button type="primary" link @click="deleteBatchHandle(scope.row.id)">Delete删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.page"
			:page-sizes="state.pageSizes"
			:page-size="state.limit"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>
		<!-- 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" :type="props.type"
		:client-id="props.clientId" :client-name="props.clientName" :mobile="props.mobile" @refreshDataList="getDataList"></add-or-update>
	</el-card>
</template>

<script setup lang="ts">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import AddOrUpdate from './date-add-or-update.vue'
import { IHooksOptions } from '@/hooks/interface'
import {  selectCustomerList } from '@/api/client'
import { parse } from 'qs'

// 客户选项下拉
const clientOptions = ref([])
const getClientList = (query: string) => {
	let req = {
		page: 1,
		limit: 100,
		customerName: query
	}
	selectCustomerList(req).then(res => {
		clientOptions.value = res.data.list;
	})
}

const props = defineProps({
	clientId: {
		type: String,
		required: true
	},
	type: {
		type: String,

	},
	clientName: {
		type: String,
		required: true
	},
	mobile: {
		type: String,
		required: true
	}
})

let customerId;

if(props.type === 'client'){
	customerId = parseInt(props.clientId);
}

const state: IHooksOptions = reactive({
	dataListUrl: '/followupDetails/page',
	deleteUrl: '/followupDetails/delete',
	queryForm: {
		customerId: customerId,
		customerName: '',
		followerName: ''
	}
})

const followUpDateFomat = (row, column) => {
	const followUpDate = row.followUpDate;
	let temp = '';
	if(followUpDate){
		temp = followUpDate.split(' ')[0]
	}
	return temp
}

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: Number) => {
	addOrUpdateRef.value.init(id)
}


const { getDataList, sizeChangeHandle, selectionChangeHandle, sortChangeHandle, currentChangeHandle, deleteBatchHandle } = useCrud(state)
</script>
